
/**
 * 从zf（面试题训练营 day4）算法题视频里摘录的
 */

 //1 数组去重和排序的多种实现算法
let ary = [12,23,12,15,25,23,25,14,16];
/*Set*/
let arr = [...new Set(ary)];
let arr1 = Array.from(new Set(ary));
// console.log(arr);
// console.log(arr1);

/*2 数组扁平化的N种实现方案*/
let arr2 = [
    [1,2,2],
    [3,4,5,5],
    [6,7,8,9,[11,12,[12,13,[14]]]],10
];
//1 flat
// arr2 = arr2.flat(Infinity);
//2
arr2 = arr2.toString().split(',').map(item=>parseFloat(item));
let arr22 = JSON.stringify(arr2).replace(/(\[|\])/g,'').split(',').map(item=>parseFloat(item));
console.log(arr22);

/**3.斐波那契数列 [1,1,2,3,5,8,13,21....];
 * fibonacci(0) -> 1
 * fibonacci(4) -> 5
 */
function fib(n){
    if(n<1) return 0;
    if(n==1 || n==2) return 1;
    let dp= new Array(n+1);
    dp[1] = dp[2] = 1;
    for(let i=3;i<=n+1;i++){
       dp[i] = dp[i-1] + dp[i-2];
    }
    return dp[n+1];
}
console.log(fib(4),'fib');
/**4.字节跳动经典算法题
 * 输入一个正数N，输出所有和为N的连续正数序列
 * 如：输入15
 * 结果：[[1,2,3,4,5],[4,5,6],[7,8]]
 */



 /**
  *
  */
